<template>
	<view :style="colorStyle">
		<view class="top-tabs">
			<view class="tabs" :class="{btborder:type == index}" v-for="(item,index) in tabsList" :key="index"
				@tap="changeTabs(index)">
				{{item.name}}
			</view> 
		</view>
		<view class='return-list items' v-if="orderList.length">
			<view class='item' v-for="(item,index) in orderList" :key="index"
				@click='goUserDetails(item)'>
				<template v-if="type == 0">
					<view class="top">
						<view class="sn_no">{{item.travel.start_city}} {{item.travel.start_district}}-{{item.travel.end_city}} {{item.travel.end_district}}</view>
						<view class="status" v-if="item.status == 0">未开始</view>
						<view class="status" v-if="item.status == 1">已开始</view>
						<view class="status" v-if="item.status == 2">已完成</view>
						<view class="status" v-if="item.status == 3">已取消</view>
					</view>
					<view class="content">
						<view class="section">时间：{{item.travel.start_time|formatDate}}</view>
						<view class="section">座位：{{item.num}}座</view>
						<view class="section">备注：{{item.remark}}</view>
					</view>
				</template>
				<template v-else>
					<view class="top">
						<view class="sn_no">{{item.start_city}} {{item.start_district}}-{{item.end_city}} {{item.end_district}}</view>
						<view class="status" v-if="item.status == 0">未开始</view>
						<view class="status" v-if="item.status == 1">已开始</view>
						<view class="status" v-if="item.status == 2">已完成</view>
						<view class="status" v-if="item.status == 3">已取消</view>
					</view>
					<view class="content">
						<view class="section">时间：{{item.start_time|formatDate}}</view>
						<view class="section">乘客：{{item.location-item.has_location}}人</view>
						<view class="section">备注：{{item.remark}}</view>
					</view>
				</template>
			</view>
		</view>
		<view class='loadingicon acea-row row-center-wrapper' v-if="orderList.length > 0">
			<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
		</view>
		<view v-if="orderList.length == 0  && !loading">
			<emptyPage :title="$t(`暂无行程~`)"></emptyPage>
		</view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
	</view>
</template>

<script>
	import home from '@/components/home';
	import emptyPage from '@/components/emptyPage';
	import {user_my_travel_list,travel_list} from '@/api/travel.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import colors from '@/mixins/color.js';
	export default {
		components: {
			home,
			emptyPage,
			// #ifdef MP
			authorize
			// #endif
		},
		mixins: [colors],
		data() {
			return {
				type: 0,
				loading: false,
				loadend: false,
				loadTitle: this.$t(`加载更多`), //提示语
				orderList: [], //订单数组
				orderStatus: -3, //订单状态
				page: 1,
				limit: 10,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				tabsList: [{
					key: 0,
					name: this.$t(`我是乘客`)
				},
				{
					key: 1,
					name: this.$t(`我是车主`)
				}]
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getDataList();
					}
				},
				deep: true
			}
		},
		onLoad(options) {
			console.info(options.type)
			if(options.type == undefined){
				this.type = 0;
			}else{
				this.type = options.type;
			}
			if (this.isLogin) {
				this.getDataList();
			} else {
				toLogin();
			}
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			console.info('加载更多')
			this.getDataList();
		},
		methods: {
			onLoadFun() {
				this.getDataList();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 去用户行程详情
			 */
			goUserDetails: function(item) {
				if(this.type == 0){
					uni.navigateTo({
						url: '/pages/main/travel/user_detail?id=' + item.id
					})
				}else{
					uni.navigateTo({
						url: '/pages/main/travel/driver_detail?id=' + item.id
					})
				}
			},
			changeTabs(index) {
				console.info(index)
				this.type = index
				this.loadend = false;
				this.page = 1
				this.limit = 10
				this.orderList = []
				this.getDataList(index)
			},
			/**
			 * 获取列表
			 */
			getDataList(type) {
				let that = this;
				if (that.loading) return;
				if (that.loadend) return;
				uni.showLoading({
					title:'加载中...'
				});
				that.loading = true;
				that.loadTitle = "";
				if(that.type == 0){
					user_my_travel_list({
						page: that.page,
						limit: that.limit
					}).then(res => {
						uni.stopPullDownRefresh();
						uni.hideLoading();
						console.info(res)
						let list = res.data.list || [];
						let loadend = list.length < that.limit;
						if(that.page == 1){
							that.orderList = list;
							that.$set(that, 'orderList', that.orderList);
						}else{
							that.orderList = that.orderList.concat(list);
							that.$set(that, 'orderList', that.orderList);
						}
						that.loadend = loadend;
						that.loading = false;
						that.loadTitle = loadend ? that.$t(`我也是有底线的`) : that.$t(`加载更多`);
						that.page = that.page + 1;
					}).catch(err => {
						uni.stopPullDownRefresh();
						uni.hideLoading();
						that.loading = false;
						that.loadTitle = that.$t(`加载更多`);
					});
				}else{
					travel_list({
						page: that.page,
						limit: that.limit
					}).then(res => {
						uni.stopPullDownRefresh();
						uni.hideLoading();
						console.info(res)
						let list = res.data.list || [];
						let loadend = list.length < that.limit;
						if(that.page == 1){
							that.orderList = list;
							that.$set(that, 'orderList', that.orderList);
						}else{
							that.orderList = that.orderList.concat(list);
							that.$set(that, 'orderList', that.orderList);
						}
						that.loadend = loadend;
						that.loading = false;
						that.loadTitle = loadend ? that.$t(`我也是有底线的`) : that.$t(`加载更多`);
						that.page = that.page + 1;
					}).catch(err => {
						uni.stopPullDownRefresh();
						uni.hideLoading();
						that.loading = false;
						that.loadTitle = that.$t(`加载更多`);
					});
				}
				
			}
		},
		 //时间戳的处理    
		filters: {
			formatDate: function(value) {
				var start = '';
				var date = new Date(value);
				if(date.getDate() === new Date().getDate()){
					start = '今天';
				}else if(date.getDate() === (new Date().getDate() + 1)){
					start = '明天';
				}else{
					var month = date.getMonth() + 1;
					var d = date.getDate();
					if(d < 10){
						d = '0' + d;
					}
					start = month + '月' + d + '日';
				}
				
				var hours = date.getHours();
				if (hours < 10)
					hours = "0" + hours;
				var minutes = date.getMinutes();
				if (minutes < 10)
					minutes = "0" + minutes;
				var time = hours + ":" + minutes;
				if(date.getFullYear() < new Date().getFullYear()){
					start = date.getFullYear() + '年' +start;
				}
				return start + ' ' +time;
			}
 
		},
	}
</script>

<style lang="scss" scoped>
	.items{
		display: flex;
		flex-direction: column;
		.item{
			background-color: white;
			display: flex;
			flex-direction: column;
			padding: 30rpx;
			border-radius: 10rpx;
			margin-top: 13rpx;
			.top{
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				height: 50rpx;
				border-bottom: 1rpx solid #f1f1f1;
				.sn_no{
					font-size: 28rpx;
					color: darkgray;
				}
				.status{
					font-size: 28rpx;
					color: indianred;
				}
			}
			
			.content{
				margin-top: 30rpx;
				
				.section{
					padding-bottom: 30rpx;
				}
			}
			
		}//item end
	}
	
	.top-tabs {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 80rpx;
		background-color: #fff;
	}

	.top-tabs .tabs {
		position: relative;
		height: 100%;
		padding: 12px 0;
	}

	.btborder {
		&::after {
			position: absolute;
			content: ' ';
			width: 39px;
			height: 2px;
			background-color: var(--view-theme);
			bottom: 2px;
			left: 50%;
			margin-left: -19px;
		}
	}
</style>
